package com.why.getwayserver.filter;

import com.why.pojo.Account;
import com.why.pojo.EmployeesAccount;
import com.why.pojo.Functionality;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.net.URI;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class TokenFilter implements GlobalFilter, Ordered {

    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String token =exchange.getRequest().getHeaders().getFirst("token");
        System.out.println(exchange.getRequest().getURI());
        //路径拦截
        String string = exchange.getRequest().getURI().toString();
        String url = string.substring(string.lastIndexOf("/") + 1);


        if (url.length()>10){
            String a=url.substring(0,10);
            if (a.equals("return_url")){
                System.out.println("支付异步回调不拦截！！！");
                return chain.filter(exchange);
            }
        }

        if (url.equals("notify_url")){
            System.out.println("支付异步回调不拦截！！！");
            return chain.filter(exchange);
        }
        if (url.equals("login")){
            System.out.println("登录不拦截！！！");
            return chain.filter(exchange);
        }
        if (url.equals("register")){
            System.out.println("注册不拦截！！！");
            return chain.filter(exchange);
        }
        if (url.equals("logout")){
            System.out.println("注销不拦截！！！");
            return chain.filter(exchange);
        }

        //权限拦截
        String s = exchange.getRequest().getHeaders().getFirst("url");
        String auth="";
        if(s!=null){
            auth=s.substring(1);
            System.out.println(auth);
        }


        boolean flag=false;
        if (token==null || token.length()==0 || token.equals("null")){
            System.out.println("token为空，不能通过");
            exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
            return exchange.getResponse().setComplete();
        }else {
            HashMap<String, Object> map = (HashMap<String, Object>)redisTemplate.opsForValue().get(token);
            if (map==null){
                System.out.println("用户已过期，请重新登录！");
                exchange.getResponse().setStatusCode(HttpStatus.FORBIDDEN);
                return exchange.getResponse().setComplete();
            }else if (map.get("user") instanceof EmployeesAccount){
                if (!auth.equals("index.html")){
                    EmployeesAccount employeesAccount=(EmployeesAccount) map.get("user");
                    List<Functionality> list =(List<Functionality>) redisTemplate.opsForValue().get(employeesAccount.getPositionId().toString());
                    for (int i = 0; i <list.size() ; i++) {
                        if (list.get(i).getUrl()!=null){
                            if (auth.equals(list.get(i).getUrl())){
                                flag=true;
                            }
                        }
                    }
                    if (!flag){
                        System.out.println("权限不足，无法访问！");
                        exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
                        return exchange.getResponse().setComplete();
                    }
                }
            }
            Date date= (Date)map.get("time");
            long temp=new Date().getTime()-date.getTime();
            System.out.println("-------------------------------------------->"+temp);
            if (temp>25*60*1000){
                redisTemplate.opsForValue().set(token,map,30, TimeUnit.MINUTES);
                map.put("time",new Date());
            }
        }
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
